package com.epfresh.pickerview.dialog;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.epfresh.pickerview.adapter.Validitypricedapter;
import com.epfresh.pickerview.utils.RecyclerItemDecoration;
import com.epfresh.supplier.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品有效期的dialog
 */

public class ValiditypriceDialog extends Dialog {

    private TextView tv_ok, tv_cancle;
    private ClickOkListener listener;
    private List<String> listdate = new ArrayList<>();
    private RecyclerView recyerview;
    private Context context;
    private Validitypricedapter validitypricedapter;

    public interface ClickOkListener {
        public void clickOk();

        public void clickcancle();
    }

    public void setOkListener(ClickOkListener listener) {
        this.listener = listener;
        this.context = context;
    }

    public ValiditypriceDialog(Context context, List<String> listdates) {
        super(context);
        this.context = context;
        listdate.clear();
        if (listdates != null) {
            listdate.addAll(listdates);
        }

    }

    protected ValiditypriceDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//设置无标题
        this.setCancelable(false);
        setContentView(R.layout.dialog_validityprice);
        tv_cancle = (TextView) findViewById(R.id.tv_cancle);
        tv_ok = (TextView) findViewById(R.id.tv_ok);
        recyerview = (RecyclerView) findViewById(R.id.recyerview);
        recyerview.addItemDecoration(new RecyclerItemDecoration(30, 2));
        recyerview.setLayoutManager(new GridLayoutManager(context, 2, GridLayoutManager.VERTICAL, false));
        if (validitypricedapter == null) {
            validitypricedapter = new Validitypricedapter(context, listdate);
        }
        recyerview.setAdapter(validitypricedapter);
        validitypricedapter.setOnItemClickListener(new Validitypricedapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                validitypricedapter.setSelectedPosition(position);
                validitypricedapter.notifyDataSetChanged();
            }
        });
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener == null) {
                    dismiss();
                } else {
                    listener.clickcancle();
                    dismiss();
                }
            }
        });
        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener == null) {
                } else {
                    listener.clickOk();
                }
            }
        });
    }

    @Override
    public void show() {
        super.show();
        getWindow().setAttributes(withAndHeight(getWindow().getAttributes()));
    }

    protected WindowManager.LayoutParams withAndHeight(WindowManager.LayoutParams params) {
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        return params;
    }

    public void setSelectedPositionAdapter(int selectedPosition) {
        if (validitypricedapter != null) {
            validitypricedapter.setSelectedPosition(selectedPosition);
            validitypricedapter.notifyDataSetChanged();
        }
    }

    public Validitypricedapter getAdapter() {
        if (validitypricedapter != null) {
            return validitypricedapter;
        } else {
            return null;
        }
    }

    public void setDataupdate(List<String> data) {
        listdate.clear();
        if (data != null) {
            listdate.addAll(data);
        }

        if (validitypricedapter != null) {
            validitypricedapter.notifyDataSetChanged();
        }
    }

}
